﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
namespace ITXMOV
{
    public partial class MainPage : UserControl
    {
        OracleDBClient WCFClient = new OracleDBClient();
        public MainPage()
        {
            InitializeComponent();
            WCFClient.GetInfoCompleted+=WCFClient_GetInfoCompleted; 
        }

        void WCFClient_GetInfoCompleted(object sender, GetInfoCompletedEventArgs e)
        {
            try
            { 
                if (e.Result == null)
                    return;
                ImageResult result = e.Result;
                string str0 = "";
                txt1.Text = result.str1;
                //MessageBox.Show(result.str1);
                //MessageBox.Show(result.str0);
                if (result.str0 == null)
                    return;
                if (result.str0.Length<20)
                    return;
                XDocument xdoc = XDocument.Parse(result.str0);
                var items = from item in xdoc.Elements()
                            select item;
                List<XElement> clist = items.ToList<XElement>();
                foreach (XElement item in clist)
                {
                    var itemsx = from itemx in item.Elements()
                                 select itemx;
                    foreach (XElement x in itemsx)
                    {
                        str0 += x.Name.ToString() + (string)x.Value;
                        if (str0.Length > 200)
                            break;
                    }
                }
                txt0.Text = str0;
                //MessageBox.Show(str0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        } 

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string CommandString = "SELECT * FROM ABPOS WHERE ROWNUM &lt; 2";
            string dbuser="intex";
            string intex = "intex1";
            string dbpwd = "intex";// "intexdbadmin";
            string OracleDB = "user id=" + dbuser + ";data source=" + intex + ";password=" + dbpwd + ";Unicode=True;" + "Persist Security Info=True;"; ;
            string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                   "<QueryBuilder>" +
                   "<AGENTID>01</AGENTID>" +
                   "<DBTYPE>ORACLE</DBTYPE>" +
                   "<DataTable>VKPOS</DataTable>" +
                   "<ConnectionString>" + OracleDB + "</ConnectionString>" +
                   "<CommandType>Text</CommandType>" +
                   "<CommandText>" + CommandString + "</CommandText>" + 
                   "</QueryBuilder>";
            /*XDocument xdoc = XDocument.Parse(rawMessage);
            Dictionary<string, object> dicSrcParamater = new Dictionary<string, object>();
            var items = from item in xdoc.Elements()
                        select item;
            List<XElement> clist = items.ToList<XElement>();
            foreach (XElement item in clist)
            {
                var itemsx = from itemx in item.Elements()
                             select itemx;
                foreach (XElement x in itemsx)
                {
                    dicSrcParamater.Add(x.Name.ToString(), (string)x.Value);
                    
                }
            }*/
            
            if (WCFClient.State == System.ServiceModel.CommunicationState.Created)
            {
              
                WCFClient.GetInfoAsync(rawMessage);
            } 
        } 
    }
}
